$(function () {


   var productTable = function(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {

            str += '<tr data-id="' + list[i].id + '" data-status="' + list[i].status + '">' +
                    '<td>' + list[i].title + '</td>' +
                    '<td>' + list[i].unit + '</td>' +
                    '<td>' + list[i].currentCost + '</td>' +
                    '<td>' + list[i].backIndent + '</td>' +
                    '<td>' + list[i].commodityStatusVal + '</td>' +
                    '<td>' + (list[i].star == 0 ? '否':'是') + '</td>' +
                    '<td>' + list[i].typeValue + '</td>' +
                    // '<td><img width="60px" src="' + list[i].cover + ' "/> </td>' +
                    '<td>' +
                    '<a class="btn btn-primary btn-xs btn-state" >修改状态</a> ' +
                    '<a  href="#/product/edit/'+ list[i].id +'" class="btn btn-primary btn-xs" >查看编辑</a> ' +
                    (function(){
                        if(list[i].star == 0){
                            return '<a class="btn btn-success btn-xs btn-star" data-state="1" >设为推荐</a> '
                        }else{
                            return '<a class="btn btn-danger btn-xs btn-star" data-state="0" >取消推荐</a> '
                        }
                    })() +
                    '</td>' +
                    '</tr>'
        }

        return str
    }

    var layerState =function() {
        return '<div style="padding: 10px 0"> ' +


                '<div class="form-group">' +
                '<label class="col-sm-4 control-label">修改状态:<span class="asterisk">*</span></label>' +
                '<div class="col-sm-7">' +
                '<select class="form-control" name="status" >'+
                    '<option value="0">上架中</option>'+
                    '<option value="-1">下架中</option>'+
                '</select>' +
                '</div>' +
                '</div>' +

                '</div>'
    }




    var productPage = new PageCreate({
        url:'/commodity/doPageQuery',
        container:$("#table tbody"),
        pagination:$("#page"),
        success:function (container,data,body) {
            container.html(productTable(data.list))
                .find(".btn-state").off("click").on("click",function () {
                var pId = $(this).parents('tr').attr("data-id");
                var status = $(this).parents('tr').attr("data-status");
                $.rootOpenDialog({
                    title: "修改状态",
                    btn: ["保存"],
                    content: layerState(),
                    success: function (lay, index) {
                        $(lay).find('.form-control[name="status"]').val(status)
                    },
                    yes: function (index, lay) {
                        // var $lay = $(lay)
                        var yesLoad = layer.load(0);
                        $.restPost('/commodity/doUpdate', {
                            token: window.localStorage.getItem('token'),
                            id:pId,
                            status:$('.form-control[name="status"]').val()
                        }).done(function (msg, data) {
                            layer.close(yesLoad)
                            $.rootSuccessDialog(msg, 2000,function () {
                                layer.close(index)
                                productPage.init(body)
                            })
                        }).fail(function (msg) {
                            layer.close(yesLoad)
                            $.rootErrorDialog(msg, 2000)
                        })
                    }
                });
            });

            $('.btn-star').off('click').on('click',function(){
                var $this = $(this);
                var yesLoad = layer.load(0);
                $.restPost('/commodity/doUpdate',{
                    star:$this.data('state'),
                    id:$this.parents('tr').data('id')
                }).done(function(msg,data){
                    layer.close(yesLoad)
                    $.rootSuccessDialog(msg, 1500,function () {
                        layer.close(yesLoad);
                        init();
                    })
                }).fail(function(msg){
                    layer.close(yesLoad);
                    $.rootErrorDialog(msg, 1500)
                });

            });
        }
    })

    var searchPage =  function () {
        var $formQuery =$(".form-query");


        function queryDatum() {
            productPage.init({
                pageNum: 1,
                pageSize: 10,
                token: window.localStorage.getItem('token'),
                title:$formQuery.find('.form-control[name=title]').val() || "",
            });
        }

        function resetDatum() {
            $formQuery.find('.form-control[name=title]').val("")
            productPage.init({
                pageNum: 1,
                pageSize: 10,
                token: window.localStorage.getItem('token')
            })
        }

        $("#queryDatum").off("click").on("click", function () {
            queryDatum()
        });

        $("#resetDatum").off("click").on("click", function () {
            resetDatum()
        });
    }


    var init = function init() {

        productPage.init({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token')
        })

        searchPage()
    }


    init()
});
